package fr.cnam.ratpProject.client.activities.itinarySearch;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.cnam.ratpProject.client.activities.itinarySearch.ItinarySearch.ItinarySearchView;
import fr.cnam.ratpProject.sharedDatas.Station;

public class ItinarySearchViewImpl extends Composite implements ItinarySearchView{

	private VerticalPanel main = new VerticalPanel();
	
	private MultiWordSuggestOracle suggest = new MultiWordSuggestOracle(); 

	public ItinarySearchViewImpl(){
		initWidget(main);
		main.setSpacing(10);
	}

	@Override
	public void buildUi() {
		main.clear();
		FlexTable content = new FlexTable();
		content.setWidget(0, 0, new Label("Station de départ"));
		final SuggestBox depart = new SuggestBox(suggest);
		content.setWidget(0, 1, depart);
		content.setWidget(1, 0, new Label("Station d'arrivée"));
		final SuggestBox arrive = new SuggestBox(suggest);
		content.setWidget(1, 1, arrive);
		Button recherche =  new Button("Rechercher l'itinéraire");
		recherche.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				buildResultUi(depart.getText(), arrive.getText());
			}
		});
		content.setWidget(2, 0, recherche);
		main.add(content);

	}
	
	private void buildResultUi(String depart, String arrive){
		main.clear();
		FlexTable content = new FlexTable();
		content.setWidget(0, 0, new Label("Durée totale du trajet : 15 minutes"));
		content.setWidget(1, 0, new Label("Station de départ : " + depart));
		content.setWidget(2, 0, new Label("Ligne 14 direction Saint Lazarre"));
		content.setWidget(2, 1, new Label("Durée 8 minutes"));
		content.setWidget(3, 0, new Label("Changement : Madeleine"));
		content.setWidget(3, 1, new Label("Durée 3 minutes"));
		content.setWidget(4, 0, new Label("Ligne 8 direction Balard"));
		content.setWidget(4, 1, new Label("Durée 4 minutes"));
		content.setWidget(5, 0, new Label("Station d'arrivée : " + arrive));
		main.add(content);
	}
	
	@Override
	public void setSuggestStations(ArrayList<Station> result) {
		suggest.clear();
		if(result !=null)
		{
			for(Station station:result)
			{
				suggest.add(station.getName());
			}
				
		}
	}

}
